//
//  MainTableViewCell_OptionView.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/30.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MainTableViewCell_OptionView.h"
#import "HomeModel.h"
#import "Common.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface MainTableViewCell_OptionView ()
@property (nonatomic, copy) NSArray *dataArr;
@end


@implementation MainTableViewCell_OptionView
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier data:(NSArray *)data{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _dataArr = data;
        [self initScrollView];
    }
    return self;
}


- (void)initScrollView {
    CGFloat scrollWidth = [UIScreen mainScreen].bounds.size.width / 2;
    CGFloat scrollHeight = OPTION_HEIGHT;
    CGFloat margin = 10;
    UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, OPTION_HEIGHT)];
    scrollV.contentSize = CGSizeMake(scrollWidth *_dataArr.count + margin *_dataArr.count, scrollHeight);
    for (int i = 0; i < _dataArr.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(margin *i + margin +scrollWidth * i, 0, scrollWidth, scrollHeight)];
        HomeModel_Option *model_o = _dataArr[i];
        [image sd_setImageWithURL:[NSURL URLWithString:model_o.img] placeholderImage:nil];
        image.userInteractionEnabled = YES;
        image.layer.cornerRadius = 5;
        image.layer.masksToBounds = YES;
        image.layer.borderWidth = 0.2;
        image.tag = 100 + i;
        image.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [scrollV addSubview:image];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        tap.numberOfTapsRequired = 1;
        [image addGestureRecognizer:tap];
    }
    [self addSubview:scrollV];
}


- (void)click:(id )sender {
    
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    UIImageView *img = (UIImageView *)tap.view;
    int tagCliked = (int)img.tag;
    
    switch (tagCliked) {
        case 100:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[0];
                self.pushBlock(model.sID,model.title);
            }
            break;
        case 101:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[1];
                self.pushBlock(model.sID,model.title);
            }
            break;
        case 102:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[2];
                self.pushBlock(model.sID,model.title);
            }
            break;
        case 103:
            if (self.pushBlock != nil) {
                HomeModel_Option *model = _dataArr[3];
                self.pushBlock(model.sID,model.title);
            }
            break;
            
        default:
            break;
    }
    
}
@end
